//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();
        egret.ImageLoader.crossOrigin = "anonymous";
        //生命周期事件
        egret.lifecycle.addLifecycleListener(context => { });
        egret.lifecycle.onPause = () => { };
        egret.lifecycle.onResume = () => { };
        //素材和主题适配器
        egret.registerImplementation("eui.IAssetAdapter", new hjc.AssetAdapter());
        egret.registerImplementation("eui.IThemeAdapter", new hjc.ThemeAdapter());
        //启动游戏
        this._launch().catch(e => console.log(e));
    }

    private async _launch(): Promise<any> {
        await this._loadResource();     //1、加载所有资源
        await this._initGame();         //2、加载游戏
        this._launchPlatform();         //3、启动平台     
        this._launchGame()
    }

    // private loading: hjc.Loading;
    private async _loadResource(): Promise<any> {
        await RES.loadConfig("resource/game.res.json", "resource/");
        await new Promise((resolve, reject) => new eui.Theme("resource/game.thm.json", this.stage)
            .addEventListener(eui.UIEvent.COMPLETE, () => resolve(), this));
        await soccer.App.loadResource()
        await RES.loadGroup("main")
        // await RES.loadGroup('asm');
        // await RES.loadGroup('mapmaker');
        await this._loadGameConfigs();
    }

    private async _loadGameConfigs() {
        await soccer.App.loadConfig()
    }

    private async _initGame(): Promise<any> {
        let proxies = soccer.App.Proxies()
        proxies.concat(comp.App.Proxies())
        proxies.concat(sight.App.Proxies())
        let controllers = soccer.App.Controllers()
        controllers.concat(comp.App.Controllers())
        controllers.concat(sight.App.Controllers())
        
        game.launch({
            configs: {
                width: 750,
                height: 1334,
            },
            ecs: true,
            Proxies: proxies,
            Controllers: controllers
        });
    }

    private _launchPlatform(): void {
        let thm = egret.getImplementation('eui.Theme') as eui.Theme;
        hjc.SkinViews.forEach(SkinView => thm.mapSkin(egret.getQualifiedClassName(SkinView), egret.getQualifiedClassName(SkinView) + 'Skin'));
    }

    private _launchGame(): void {
        game.controller(sight.MainController).launchGame()
    }
}